<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sing extends MY_Controller {
    
    function Sing()
    {
        parent::__construct();
        $this->load->model('song_mod');
        $this->_checktoken();
    }
    
    public function browse()
    {
        $result = array();
        $orderby = $this->input->post('orderby');
        if(!$orderby) {$orderby = 'insert_date desc';}
        
        $datasong  = $this->song_mod->get_songs($orderby);
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function latest20()
    {
        $this->output->enable_profiler(FALSE);
        
        $result = array();
        $orderby = 'insert_date desc';
        $offset = 0;
        $limit = 20;
        
        $datasong  = $this->song_mod->get_songs_limit($orderby,$offset,$limit);
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function latest()
    {
        $this->output->enable_profiler(FALSE);
        
        $result = array();
        $orderby = 'insert_date desc';
        $offset = 0;
        $limit = $this->input->post('limit');
        
        if(empty($limit)) { $limit = 20; }
            
        $datasong  = $this->song_mod->get_songs_limit($orderby,$offset,$limit);
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function search()
    {
        $result = array();
        $keyword = $this->input->post('keyword');
        $field = $this->input->post('field');
        if(empty($field)) {$field = 'title';}
        
        $datasong  = $this->song_mod->search_song($keyword, $field);
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function user_playlist()
    {
        $result = array();
        $username = $this->session->userdata('username');
        if($username) {
            $datasong  = $this->song_mod->get_playlist($username);
            if($datasong) {
                $result['result'] = TRUE;
                $result['songs'] = $datasong;
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function user_record()
    {
        $result = array();
        $datasong  = $this->song_mod->get_records();
        if($datasong) {
            $result['result'] = TRUE;
            $result['songs'] = $datasong;
        } else {
            $result['result'] = FALSE;
        }
        $this->_outputjson($result);
    }
    
    public function play_video()
    {
        $result = array();
        $idsong = $this->input->post('idsong');
        $username = $this->session->userdata('username');
        if(!empty($idsong) && !empty($username)) {
            $dataplay  = $this->song_mod->get_song($idsong,'video');
            if($dataplay) {
                //Set URL
                //$url = $this->config->item('server_rtsp').$dataplay['path_file'];
                //$dataplay['url'] = $url;
$rtsp = $this->song_mod->_getRTSP($idsong);
                $dataplay['rtsp_video'] = $rtsp['video'];
                $dataplay['rtsp_audio'] = $rtsp['audio'];                
$result['song'] = $dataplay;
                $result['result'] = TRUE;
                //Insert Action Log
                $this->song_mod->add_log('sing', $idsong, $username);
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function play_audio()
    {
        $result = array();
        $idsong = $this->input->post('idsong');
        $username = $this->session->userdata('username');
        if(!empty($idsong) && !empty($username)) {
            $dataplay  = $this->song_mod->get_song($idsong,'audio');
            if($dataplay) {
                //Set URL
                //$url = $this->config->item('server_rtsp').$dataplay['path_file'];
                //$dataplay['url'] = $url;
 				$rtsp = $this->song_mod->_getRTSP($idsong);
                $dataplay['rtsp_video'] = $rtsp['video'];
                $dataplay['rtsp_audio'] = $rtsp['audio'];                $result['song'] = $dataplay;
                $result['result'] = TRUE;
                //Insert Action Log
                $this->song_mod->add_log('sing', $idsong, $username);
            } else {
                $result['result'] = FALSE;
            }
            $this->_outputjson($result);
        }
    }
    
    public function upload_record_video()
    {
        $result = array();
        $username = $this->session->userdata('username');
        $idsong = $this->input->post('idsong');
        if(!empty($username) & !empty($idsong)) {
            $config['upload_path'] = './uploads/record/';
            $config['allowed_types'] = 'mp4|mkv';
            $this->load->library('upload', $config);
            if($this->upload->do_upload('recorded')) {
                $datafile = $this->upload->data();
                $data_post = array (
                    'username' => $username,
                    'id_song' => $idsong,
                    'type' => 'video',
                    'path_file' => $datafile['full_path'],
                    'insert_date' => date('Y-m-d H:i:s')
                );
                $this->song_mod->add_record($data_post);
                //Insert Action Log
                $this->song_mod->add_log('record', $idsong, $username);
                $result['result'] = TRUE;
            } else {
                $result['result'] = FALSE;
                $result['msg'] = $this->upload->display_errors();
            }   
        } else {
            $result['result'] = FALSE;
            $result['msg'] = 'data blank';
        }
    }
    
    public function upload_record_audio()
    {
        $result = array();
        $username = $this->session->userdata('username');
        $idsong = $this->input->post('idsong');
        if(!empty($username) & !empty($idsong)) {
            $config['upload_path'] = './uploads/record/';
            $config['allowed_types'] = 'mp3';
            $this->load->library('upload', $config);
            if($this->upload->do_upload('recorded')) {
                $datafile = $this->upload->data();
                $data_post = array (
                    'username' => $username,
                    'id_song' => $idsong,
                    'type' => 'audio',
                    'path_file' => $datafile['full_path'],
                    'insert_date' => date('Y-m-d H:i:s')
                );
                $this->song_mod->add_record($data_post);
                //Insert Action Log
                $this->song_mod->add_log('record', $idsong, $username);
                $result['result'] = TRUE;
            } else {
                $result['result'] = FALSE;
                $result['msg'] = $this->upload->display_errors();
            }   
        } else {
            $result['result'] = FALSE;
            $result['msg'] = 'data blank';
        }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */